<?php
	require_once("Service.php");
	require_once("Database.php");
	require_once("vo/UserVO.php");
	require_once("vo/GroupVO.php");
	require_once("vo/SponsorVO.php");
	
	/**
	 * Class which handles all group browser related queries.
	 */
	class GroupBrowserService extends Service {
		
		var $conn;
		
		/** The constructor of this object. */
		function __construct() {
			
			parent::__construct();
			$this->conn = new Database();
		}
		
		/**
		 * Finds a list of groups which a user may join.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who's to filter on.
		 * @return An array of groups.
		 */
		function getGroupList($sessionID, $userUID) {
			
			$this->validateSession($sessionID);
			
			// get the groups the user may join
			$query = Database::buildQuery("
				SELECT
					Groups.groupUID,
					Groups.groupName,
					Groups.description,
					Groups.logoURL,
					CASE
						WHEN UserGroups.isManager IS NULL
						THEN 0
						ELSE UserGroups.isManager
					END AS isManager
				FROM Groups
				LEFT OUTER JOIN UserGroups
					ON UserGroups.groupUID = Groups.groupUID
					AND UserGroups.userUID = ?
				WHERE Groups.isArchived = 0
				AND (
					UserGroups.isBanned IS NULL
					OR UserGroups.isBanned = 0
				)
			", $userUID);
			$qryUserGroups = $this->conn->execute($query);
			
			$groups = array();
			while($recUserGroups = mysql_fetch_assoc($qryUserGroups)) {
				$groups[] = GroupVO::mapObject($recUserGroups);
			}
			
			return $groups;
		}
		
		/**
		 * Finds a object containing more details about a group.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $groupUID The group to find out more about.
		 * @return An object containing the extended group data.
		 */
		function getGroupDetails($sessionID, $groupUID) {
			
			$this->validateSession($sessionID);
			
			$result = array();
			
			// get the amount of user of the group
			$query = Database::buildQuery("
				SELECT COUNT(userUID) AS noMembers
				FROM UserGroups
				WHERE groupUID = ?
				AND isBanned = 0
			", $groupUID);
			$qryMembers = $this->conn->execute($query);
			
			$recMembers = mysql_fetch_assoc($qryMembers);
			$result["noMembers"] = $recMembers["noMembers"];
			
			// get the administrators of the group
			$query = Database::buildQuery("
				SELECT
					Users.userUID,
					Users.firstName,
					Users.lastName
				FROM UserGroups
				INNER JOIN Users
					ON Users.userUID = UserGroups.userUID
				WHERE UserGroups.groupUID = ?
				AND UserGroups.isManager = 1
			", $groupUID);
			$qryGroupAdmins = $this->conn->execute($query);
			
			$admins = array();
			while($recGroupAdmins = mysql_fetch_assoc($qryGroupAdmins)) {
				$admins[] = UserVO::mapObject($recGroupAdmins);
			}
			$result["admins"] = $admins;
			
			// get the sponsors of the group
			$query = Database::buildQuery("
				SELECT
					sponsorUID,
					groupUID,
					sponsorName,
					description,
					sponsorURL
				FROM GroupSponsors
				WHERE groupUID = ?
			", $groupUID);
			$qryGroupSponsors = $this->conn->execute($query);
			
			$sponsors = array();
			while($recGroupSponsors = mysql_fetch_assoc($qryGroupSponsors)) {
				$sponsors[] = SponsorVO::mapObject($recGroupSponsors);
			}
			$result["sponsors"] = $sponsors;
			
			return $result;
		}
		
		/**
		 * Adds an user as a member of a group.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who is joining the group.
		 * @param $groupUID The group to join.
		 * @return The groupUID of the group which was joined.
		 */
		function joinGroup($sessionID, $userUID, $groupUID) {
			
			$this->validateSession($sessionID);
			
			// add the user as a group member
			$query = Database::buildQuery("
				INSERT INTO UserGroups(
					userUID,
					groupUID
				) VALUES (?, ?)
			", $userUID, $groupUID);
			$this->conn->execute($query);
			
			return $groupUID;
		}
		
		/**
		 * Removes an user as a member of a group.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who is leaving the group.
		 * @param $groupUID The group to leave.
		 * @return The groupUID of the group which was left.
		 */
		function leaveGroup($sessionID, $userUID, $groupUID) {
			
			$this->validateSession($sessionID);
			
			// remove the user as a group member
			$query = Database::buildQuery("
				DELETE FROM UserGroups
				WHERE userUID = ?
				AND groupUID = ?
			", $userUID, $groupUID);
			$this->conn->execute($query);
			
			return $groupUID;
		}
	}
?>